草庐IT

Java final 与 C++ const

全部标签

c++ - 为什么 decltype 从内置类型的返回类型中删除 const?

作为一般规则,decltype保留常量:constintci=0;decltype(ci)x;//xisconstintx=5;//error--xisconstclassGadget{}:constGadgetmakeCG();//factorydecltype(makeCG())y1,y2;//y1andy2areconstGadgetsy1=y2;//error--y1isconst但对于返回基本类型的const返回类型,decltype似乎抛弃了const:constintmakeCI();//factorydecltype(makeCI())z;//zisNOTconstz=

c++ - 为什么不能实例化带有 "non const"复制构造函数的对,而没有实例化一对是可能的?

假设您有以下类(class):structA{A(){}A(A&)=delete;};intmain(){std::pairp1;return0;}以下代码将无法编译(使用-std=c++11和g++)并出现以下错误:/usr/include/c++/5/bits/stl_pair.h:Ininstantiationof‘structstd::pair’:test.cpp:13:23:requiredfromhere/usr/include/c++/5/bits/stl_pair.h:127:17:error:‘constexprstd::pair::pair(conststd::pa

C++ 标准 : Unexpected const_iterator in multiset

我最近遇到了一个奇怪的问题,在遍历多重集时,我得到的是const_iterator而不是预期的iterator。结果证明这对MSVC来说不是问题,但g++给了我一个错误:error:invalidinitializationofreferenceoftype'myPtr&'fromexpressionoftype'constboost::shared_ptr'相关代码:typedefstd::multisetmyList;myList_mystuff;voidtick(floatdt){for(myList::iteratori=_mystuff.begin();i!=_mystuff

c++ - 两个版本的函数可以重载,常量成员函数和不带 const 的函数

我刚遇到各种重载方法,如传递的参数类型、不同数量的参数、返回类型等。我只想知道我可以用以下两个版本重载函数//functionwhichcanmodifymemberString&MyClass::doSomething();//constantmemberfunctionString&MyClass::doSomething()const;请告诉我背后的原因。 最佳答案 是的,你可以。如果你有MyClassm;m.doSomething();将调用非常量版本。当你有constMyClassm;m.doSomething();将调用

c++ - C++11 中 map 标准的 at() const 访问器是什么?

我试图找出如何在const方法中从map返回一个值,我偶然发现了gcc4.6中map的at()方法。当我查看它时,我意识到它是非标准的:C++mapaccessdiscardsqualifiers(const)但它确实比find()方法要简洁得多。我想知道C++11是否已纠正此问题-at()formap是新标准的一部分吗? 最佳答案 是的。std::map在C++11中有一个at成员函数,其规范如下(23.4.4.3/9):T&at(constkey_type&x);constT&at(constkey_type&x)const;R

C++,从 ‘char’ 到 ‘const char*’ 的无效转换

在C++中我有这个程序:#includeusingnamespacestd;intmain(){stringexpression_input;cout我收到以下错误:prog.cpp:15:error:invalidconversionfrom‘char’to‘constchar*’prog.cpp:15:error:initializingargument2of‘std::basic_string&std::basic_string::insert(typename_Alloc::rebind::other::size_type,const_CharT*)[with_CharT=ch

c++ - 为什么我可以使用类型别名声明一个 const 引用?

我有一个简单的问题:据我所知,我可以声明一个指向某些数据类型的const指针或一个指向常量数据类型的指针,但我只能声明一个对常量数据类型的引用,而不能声明常量引用数据类型;引用已经是常量的事实,因为它不能重新绑定(bind)到另一个对象。因此,当我尝试创建一个constreftosomeDataType时,我遇到了编译时错误。但对我来说重要的是当使用typedef或using与typealias一起使用时。例如:#includeintmain(){inti{10};//int&constr1{i};//error:‘const’qualifierscannotbeappliedto‘i

c++ - 为什么在对基类的 const 引用上调用派生类的析构函数?

在GMan'sanswerhere,restore_base类的析构函数不是virtual,所以我一直想知道它究竟是如何工作的。通常您希望restorer_base的析构函数仅在对象超出范围后执行,但派生的restorer_holder析构函数似乎真的被调用了。有谁愿意赐教吗? 最佳答案 需要虚拟析构函数的标准情况是voidfoo(){scoped_ptrobj=factory_returns_a_Derived();//...use'obj'here...}而您不的标准情况是voidfoo(){Derivedobj;//...us

c++ - 错误 C2280 : 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function

我在尝试创建使用C++11标准线程的VC++静态库时遇到问题。我目前有两个类,我可以在我的起始类(最后声明的)上声明并稍后定义一个线程。在这个阶段,代码只是一个套接字监听器,然后创建另一个类的对象来处理每个接受的客户端。这些子对象应该创建并行数据捕获、编码和传输所需的线程。问题是:如果我在我的其他类上声明了一个std::thread,即使我在我的起始类上所做的完全一样,无论如何,我在构建errorC2280时遇到这个错误:'std::thread::thread(conststd::thread&)':试图引用已删除的函数[...]\vc\include\functional11241

c++ - 不理解 C++ 类型不匹配 : const Foo* to Foo* const&

拥有这组对象和语句:QSetset;iteratorQSet::insert(constT&value)//typeofthefunctionIwanttocallconstFoo*get()const//typeofthefunctionIusetogettheargumentset.insert(get());//thelineshowingupaserror我收到错误“参数1没有从‘constFoo*’到‘Foo*const&’的已知转换”。我想我在阅读这些类型时遇到了麻烦,因为我不知道我应该怎么做才能完成这项工作。根据我的阅读,const关键字适用于其左侧的类型,但顶级cons